import Vue from 'vue'

export function getCurrentDateTime(format) {
    const now = new Date();
    const year = now.getFullYear();
    const month = String(now.getMonth() + 1).padStart(2, '0');
    const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');

    let result = format.replace('YYYY', year);
    result = result.replace('MM', month);
    result = result.replace('DD', day);
    result = result.replace('HH', hours);
    result = result.replace('mm', minutes);
    result = result.replace('ss', seconds);

    return result;
}
export function formatTimestamp(timestamp, format) {
    const date = new Date(timestamp);

    const year = date.getFullYear();
    const month = ("0" + (date.getMonth() + 1)).slice(-2);
    const day = ("0" + date.getDate()).slice(-2);
    const hours = ("0" + date.getHours()).slice(-2);
    const minutes = ("0" + date.getMinutes()).slice(-2);
    const seconds = ("0" + date.getSeconds()).slice(-2);

    let formattedDate;

    switch (format) {
        case "YYYY":
            formattedDate = `${year}`;
            break;
        case "YYYY:MM":
            formattedDate = `${year}-${month}`;
            break;
        case "YYYY:MM:DD":
            formattedDate = `${year}-${month}-${day}`;
            break;
        case "YYYY:MM:DD HH":
            formattedDate = `${year}-${month}-${day} ${hours}`;
            break;
        case "YYYY:MM:DD HH:mm":
            formattedDate = `${year}-${month}-${day} ${hours}:${minutes}`;
            break;
        case "YYYY:MM:DD HH:mm:ss":
            formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
            break;
        default:
            formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
            break;
    }

    return formattedDate;
}


export function deepCopy(target) {
    if (typeof target == 'object') {
        const result = Array.isArray(target) ? [] : {}
        for (const key in target) {
            if (typeof target[key] == 'object') {
                result[key] = deepCopy(target[key])
            } else {
                result[key] = target[key]
            }
        }

        return result
    }

    return target
}

export function swap(arr, i, j) {
    const temp = arr[i]
    Vue.set(arr, i, arr[j])
    Vue.set(arr, j, temp)
}

export function $(selector) {
    return document.querySelector(selector)
}

const components = ['VText', 'RectShape', 'CircleShape']
export function isPreventDrop(component) {
    return !components.includes(component) && !component.startsWith('SVG')
}
